package io.xxx.omni.oss.custom

import com.google.gson.JsonObject
import io.xxx.omni.oss.common.format
import io.xxx.omni.oss.common.toJsonString
import io.xxx.omni.oss.common.toLocalDateTime
import io.xxx.omni.oss.domain.Document
import io.xxx.omni.oss.domain.DocumentType

@Suppress("unused")
class DdRefundSynchronizer : DdSynchronizer() {

    override val documentType = DocumentType.REFUND

    override val method = "dangdang.orders.exchange.return.list.get"

    override fun getCount(context: Context, response: JsonObject): Int {
        return response["orderCancelApplyRemote"].asJsonObject["size"].asInt
    }

    override fun buildDocuments(context: Context, response: JsonObject): List<Document> {
        return response
            .getAsJsonArray("orderCancelApplyRemote")
            .map {
                it as JsonObject
                Document(
                    it["applyId"].asString,
                    it["orderId"].asString,
                    it["applyStatus"].asString,
                    it.toJsonString(),
                    it["creationDate"].toLocalDateTime(),
                    it["lastChangeDate"].toLocalDateTime(),
                )
            }
    }

    override fun buildRequest(context: Context, pageNo: Int): Map<String, Any> {
        val (startTime, endTime) = context
        return mapOf(
            "creationDateBegin" to startTime.format(),
            "creationDateEnd" to endTime.format(),
        )
    }
}